// Angular Version 6 below need to be deleted {providedIn: 'root'}

// angular
import {Injectable} from '@angular/core';
import {HttpParams} from '@angular/common/http';
// owner
import {Request} from 'pengesoft-ng-lib';
// models
import {Customer, CustomerList} from '../domains/customer.domain';
import { HouseList } from '../domains/houselist.domain';

/**
 *
 */
@Injectable({
  providedIn: 'root'
})
export class CustomerServiceSvr {
  private readonly baseUrl: string;

  constructor(
    private request: Request
  ) {
    this.baseUrl = 'customerServiceSvr.assx/';
  }


  updateCustomerPassword(token: string, customerId: string, oldPwd: string, newPwd: string, confirmPwd: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('customerId', customerId.toString())
      .append('oldPwd', oldPwd.toString())
      .append('newPwd', newPwd.toString())
      .append('confirmPwd', confirmPwd.toString());
    return this.request.post<number>(this.baseUrl + 'updateCustomerPassword', httpParams).then((res) => {
      return res;
    });
  }

  updateCustomerAvatar(token: string, customerId: string, memoryFile: File): Promise<string> {
    const httpParams = new FormData();
    httpParams.append('token', token.toString());
    httpParams.append('customerId', customerId.toString());
    httpParams.append('memoryFile', memoryFile);
    return this.request.post<string>(this.baseUrl + 'updateCustomerAvatar', httpParams).then((res) => {
      return res;
    });
  }


  listCustomers(token: string, name: string, vipId: string, authentication: number, skip: number, size: number): Promise<CustomerList> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('name', name.toString())
      .append('vipId', vipId.toString())
      .append('authentication', authentication.toString())
      .append('skip', skip.toString())
      .append('size', size.toString());
    return this.request.post<CustomerList>(this.baseUrl + 'listCustomers', httpParams).then((res) => {
      return new CustomerList(res);
    });
  }

  listHousesByCustomerId(token: string, customerId: string, skip: number, size: number): Promise<HouseList> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('customerId', customerId.toString())
      .append('skip', skip.toString())
      .append('size', size.toString());
    return this.request.post<HouseList>(
      this.baseUrl + 'listHousesByCustomerId', httpParams
    ).then(res => new HouseList(res));
  }


  saveCustomer(phone: string, code: string, password: string, confirmPassword: string, nickName: string, sex: number, birthday: Number, email: string, address: string, avatar: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('phone', phone.toString())
      .append('code', code.toString())
      .append('password', password.toString())
      .append('confirmPassword', confirmPassword.toString())
      .append('nickName', nickName.toString())
      .append('sex', sex.toString())
      .append('birthday', birthday.toString())
      .append('email', email.toString())
      .append('address', address.toString())
      .append('avatar', avatar.toString());
    return this.request.post<number>(this.baseUrl + 'saveCustomer', httpParams).then((res) => {
      return res;
    });
  }


  cancellation(token: string, customerId: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('customerId', customerId.toString());
    return this.request.post<number>(this.baseUrl + 'cancellation', httpParams).then((res) => {
      return res;
    });
  }


  GetVersion(): Promise<string> {
    const httpParams = new HttpParams();
    return this.request.post<string>(this.baseUrl + 'GetVersion', httpParams).then((res) => {
      return res;
    });
  }


  getCustomerByPrimaryKey(token: string, customerId: string): Promise<Customer> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('customerId', customerId.toString());
    return this.request.post<Customer>(this.baseUrl + 'getCustomerByPrimaryKey', httpParams).then((res) => {
      return new Customer(res);
    });
  }


  forgotPwd(phoneNumber: string, verification: string, newPwd: string, confirmPwd: string, flag: number, email: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('phoneNumber', phoneNumber.toString())
      .append('verification', verification.toString())
      .append('newPwd', newPwd.toString())
      .append('confirmPwd', confirmPwd.toString())
      .append('flag', flag.toString())
      .append('email', email.toString());
    return this.request.post<number>(this.baseUrl + 'forgotPwd', httpParams).then((res) => {
      return res;
    });
  }


  updateCustomer(token: string, customerId: string, sex: number, birthday: Number, nickName: string, email: string, address: string, avatar: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('customerId', customerId.toString())
      .append('sex', sex.toString())
      .append('birthday', birthday.toString())
      .append('nickName', nickName.toString())
      .append('email', email.toString())
      .append('address', address.toString())
      .append('avatar', avatar.toString());
    return this.request.post<number>(this.baseUrl + 'updateCustomer', httpParams).then((res) => {
      return res;
    });
  }


  updateAuthenticationInfo(token: string, customerId: string, name: string, idCard: string): Promise<number> {
    const httpParams = new HttpParams()
      .append('token', token.toString())
      .append('customerId', customerId.toString())
      .append('name', name.toString())
      .append('idCard', idCard.toString());
    return this.request.post<number>(this.baseUrl + 'updateAuthenticationInfo', httpParams).then((res) => {
      return res;
    });
  }
}
